function [h hfig] = stackplot(varargin)
% generate a stacked set of plots with x and y datasets
% (each dimension mtr x npts), and plot the reference trace on every plot
% ref must be 1 <= ref <= mtr.
%
% 7/24/2007 Paul B. Manis
%
% requires mulitiplot routine. (this is basically a wrapper for mulitplot)
%
figh = varargin{1};
x = varargin{2};
y = varargin{3};
ref = varargin{4};
if(nargin > 4)
    box = varargin{5};
else
    box = [0 0 1 1]; % full size
end;

if(ishandle(figh)) % passed a handle?
    name = get(figh, 'Tag');
else
    name = figh;
end;

u = size(x);
v = size(y);
if (~isequal(size(u), size(v)))
    fprintf(1, 'stackplot: input array sizes do not match:\n');
    return;
end;

if(ref < 1 || ref > size(u, 1))
    fprintf(1, 'stackplot: reference is outside of range of data\n');
    return;
end;

[h, hfig] = multiplot(name, 1, size(x,1),  1, box, [2 5]);

for i = 1:length(h)
    axes(h(i));
    plot(x(i,:), y(i,:), '-k');
    hold on;
    plot(x(ref,:), y(ref,:), 'color', [0.7 0.7 0.7]);
    dcm=datacursormode(gcf);
    set(dcm, 'UpdateFcn', @stackplotupdate, 'SnapToDataVertex', 'on', 'DisplayStyle', 'datatip', 'Enable', 'on');
end;


